$(function() {
    template.defaults.imports.formatDate = function(date) {
        date = new Date(date)
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        return year + '-' + month + '-' + day
    }

    getComment();

    function getComment() {
        $.ajax({
            type: 'get',
            url: 'admin/comments',
            success: function(res) {
                var content = template('tpl', res)
                $('.layui-table tbody').html(content);
            }
        })
    }


    //删除
    var layer = layui.layer;
    $('.layui-table tbody').on('click', '.delete', function(e) {
        var id = $(e.target).data('id')
        console.log(id);
        layer.confirm('确认要删除吗？', function(index) {
            $.ajax({
                type: 'delete',
                url: 'admin/comments/' + id,
                success: function(res) {
                    if (res.status === 0) {
                        layer.close(index)
                        getComment();
                    }
                }
            })
        })
    })
})